package at.jku.ssw.fortress.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.swt.SWT;

import at.jku.ssw.fortress.util.ColorManager;

public class CodeScanner extends RuleBasedScanner {

	private static String[] KEYWORDS = { "BIG", "SI", "unit", "absorbs", "abstract", "also",
			"api", "as", "asif", "at", "atomic", "bool", "case", "catch", "coerces",
			"coercion", "component", "comprises", "default", "dim", "do", "elif", "else",
			"end", "ensures", "except", "excludes", "exit", "export", "extends", "finally",
			"fn", "for", "forbid", "from", "getter", "hidden", "ident", "idiom", "if",
			"import", "in", "int", "invariant", "io", "juxtaposition", "label", "largest",
			"nat", "object", "of", "or", "opr", "private", "property", "provided", "requires",
			"self", "settable", "setter", "smallest", "spawn", "syntax", "test", "then",
			"throw", "throws", "trait", "transient", "try", "tryatomic", "type", "typecase",
			"unit", "value", "var", "where", "while", "widening", "widens", "with", "wrapped",
			"goto", "idiom", "public", "pure", "reciprocal", "static" };

	private Token defaultt;

	private Token keyword;

	private Token ascii;

	public CodeScanner(ColorManager colorManager) {
		defaultt = new Token(new TextAttribute(colorManager
				.getColor(ColorConstants.CODE_DEFAULT)));
		keyword = new Token(new TextAttribute(colorManager
				.getColor(ColorConstants.CODE_KEYWORD), null, SWT.BOLD));
		ascii = new Token(new TextAttribute(colorManager
				.getColor(ColorConstants.CODE_ABBREVIATION), null, SWT.BOLD));
		setDefaultReturnToken(defaultt);
		setRules();
	}

	private void setRules() {
		List<IRule> rules = new ArrayList<IRule>();
		AsciiRule words = new AsciiRule(new WordDetector(),defaultt, ascii);
		for (String kw : KEYWORDS) {
			words.addWord(kw, keyword);
		}
		rules.add(words);
		
		setRules(rules.toArray(new IRule[rules.size()]));
	}

}
